package com.zhongxin.wymovieticketfragment;

import com.zhongxin.wymovieticket.CitieslistActivity;
import com.zhongxin.wymovieticket.R;
import com.zhongxin.wymovieticket.location.Activity01;
import com.zhongxin.wymovieticket.location.City;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

import android.widget.TextView;

public class MovieFragment extends Fragment implements OnClickListener {

	private FragmentManager fManager;
	private FragmentTransaction ftTransaction, ftTransaction2, fTransaction3;
	private HotMovieFragment hotMovieFragment,comeMovieFragment;
	//0是热门电影，1是即将上映的电影
	private String url = "http://piao.163.com/m/movie/list.html?type=0&city=%25s&apiVer=14&mobileType=android&deviceId=08:00:27:f4:e2:00&channel=netease&ver=4.8";
	private String comemovieurl="http://piao.163.com/m/movie/list.html?type=1&city=440300&apiVer=14&mobileType=android&deviceId=08:00:27:f4:e2:00&channel=netease&ver=4.8";
	private ChangeModeFragment chanModeFragment;
	private TextView citiesbt;
	
	private RadioButton hotbt, comebt;
	private Button changeshowbt;
	private int flag = 0;
	private City city;
	
	public MovieFragment() {
		// TODO Auto-generated constructor stub
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Bundle bundle=getArguments();
		String cityname=bundle.getString("cityname2");
		View view = inflater.inflate(R.layout.fragment_movie, container, false);
		
		//city=(City) bundle.get("city");
		citiesbt = (TextView) view.findViewById(R.id.cities);
		if(cityname==null){
			citiesbt.setText("北京");
		}else{
			citiesbt.setText(cityname);
		}
		
		hotbt = (RadioButton) view.findViewById(R.id.hotmovies);
		comebt = (RadioButton) view.findViewById(R.id.comemovies);
		changeshowbt = (Button) view.findViewById(R.id.changeshow);

		citiesbt.setOnClickListener(this);
		hotbt.setOnClickListener(this);
		comebt.setOnClickListener(this);
		changeshowbt.setOnClickListener(this);

		hotMovieFragment = new HotMovieFragment();
		comeMovieFragment=new HotMovieFragment();
		hotMovieFragment.init(url);
		comeMovieFragment.init(url);
		fManager = getChildFragmentManager();
		ftTransaction = fManager.beginTransaction();
		ftTransaction.replace(R.id.fragment_movie_content, hotMovieFragment);
		ftTransaction.addToBackStack("hotMovieFragment");
		ftTransaction.commit();

		chanModeFragment = new ChangeModeFragment();

		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ftTransaction2 = fManager.beginTransaction();
		switch (v.getId()) {

		case R.id.cities:
			Intent citiesListintent = new Intent(getActivity(),
					Activity01.class);
			startActivity(citiesListintent);
			break;
		case R.id.hotmovies:
			 ftTransaction2.replace(R.id.fragment_movie_content,
			 hotMovieFragment);
			

			break;
		case R.id.comemovies:
			ftTransaction2.replace(R.id.fragment_movie_content,
					comeMovieFragment);
			 ftTransaction2.addToBackStack("comeMovieFragment");
			break;
		case R.id.changeshow:
			if (flag == 0) {
				ftTransaction2.replace(R.id.fragment_movie_content,
						chanModeFragment);
				ftTransaction2.addToBackStack("chanModeFragment");
				flag = 1;
			} else {
				ftTransaction2.replace(R.id.fragment_movie_content,
						hotMovieFragment);
				ftTransaction2.addToBackStack("hotMovieFragment");
				flag = 0;

			}

			break;
		default:
			break;
		}
		ftTransaction2.commit();
	}

}
